﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Db.Model.Merchant;
using Mate.Framework.BaseController.Admin;
using Mate.Merchant.Service;
using Microsoft.AspNetCore.Mvc;

namespace Mate.Admin.Controllers.Merchant
{
    public class MerchantTagController : BaseController
    {
        private readonly IMerchantTagService merchantTagService;
        private readonly ILog log;
        private readonly ICacheManager _cache;
        public MerchantTagController(IMerchantTagService merchantTagService, ICacheManager cache, ILog log)
        {
            this.log = log;
            this.merchantTagService = merchantTagService;
            this._cache = cache;
        }

        public IActionResult Index()
        {
            return View();
        }

        #region 获取列表
        [HttpPost]
        public IActionResult GetList(int pageIndex, int pageSize, string Key)
        {
            int recordCount = 0;
        
            var list = merchantTagService.Queryable.WhereIF(!string.IsNullOrWhiteSpace(Key), x => x.tag_name.Contains(Key))
                .OrderBy(x => x.id, SqlSugar.OrderByType.Desc)
                .ToPageList(pageIndex, pageSize, ref recordCount);
            var result = new
            {
                rows = list,
                total = recordCount
            };
            return Json(result);
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var result = new BaseResult();

            var model = merchantTagService.Get(x => x.id == id);
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }

            var b = merchantTagService.Delete(x => x.id == id);
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "删除成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "删除失败";
            }
            return Json(result);
        }
        #endregion

        #region 添加
        public ActionResult Create()
        {
            var model = new mp_merchant_tag() { is_open = 1 };
            return View(model);
        }

        [HttpPost]
        public JsonResult Create(mp_merchant_tag model)
        {
            var result = new BaseResult();
            var obj = merchantTagService.Insert(model);
            if (obj > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "添加成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "添加失败";
            }
            return Json(result);
        }
        #endregion

        #region 修改
        public ActionResult Edit(int id)
        {
            var model = merchantTagService.Get(x => x.id == id);
            if (model == null)
            {
                return Content("参数错误");
            }
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(mp_merchant_tag model)
        {
            var result = new BaseResult();
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }
            

            var b = merchantTagService.Update(model,
                x => new { x.tag_name, x.is_open },
                x => new object[] { x.id });
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "修改成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "修改失败";
            }
            return Json(result);

        }
        #endregion
    }
}